var uChartsInstance = {};

function showCharts(id,data){
    if (uChartsInstance[id]) {
        // 只对数据进行更新
        uChartsInstance[id].updateData(data);
        return;
    }
    const canvas = document.getElementById(id);
    const ctx = canvas.getContext("2d");
    const pixel = window.devicePixelRatio;
    canvas.width = canvas.offsetWidth * pixel;
    canvas.height = canvas.offsetHeight * pixel;
    uChartsInstance[id] = new uCharts({
        context: ctx,
        width: canvas.width,
        height: canvas.height,
        pixelRatio: pixel,
        type: "line",
        animation: true,
        background: "#FFFFFF",
        color: ["#1890FF","#91CB74","#FAC858","#EE6666","#73C0DE","#3CA272","#FC8452","#9A60B4","#ea7ccc"],
        padding: [15,10,0,15],
        enableScroll: false,
        legend: {},
        xAxis: {
            disableGrid: true
        },
        yAxis: {
            gridType: "dash",
            dashLength: 2
        },
        extra: {
            line: {
                type: "curve",
                width: 2,
                activeType: "hollow"
            }
        },
        // 进行合并
        // categories: data.categories,
        // series: data.series,
        ...data,
    });
    canvas.onclick = function(e) {
        uChartsInstance[e.target.id].touchLegend(getH5Offset(e));
        uChartsInstance[e.target.id].showToolTip(getH5Offset(e));
    };
    canvas.onmousemove=function(e) {
        uChartsInstance[e.target.id].showToolTip(getH5Offset(e));
    };
}

window.uploadOptions = function(data) {
    showCharts('kcbLUVWsggIzGDtPSuxQAVVwPDjOdJec', data);
};
// 在这之前必须执行uploadOptions 对表格进行初始化
window.uploadData = function(data) {
    showCharts('kcbLUVWsggIzGDtPSuxQAVVwPDjOdJec', data);
};


// 测试代码
function getServerData() {
    //模拟从服务器获取数据时的延时
    setTimeout(() => {
        //模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
        let res = {
            categories: ["2018","2019","2020","2021","2022","2023"],
            series: [
                {
                    name: "成交量A",
                    lineType: "dash",
                    data: [35,8,25,37,4,20]
                },
                {
                    name: "成交量B",
                    data: [70,40,65,100,44,68]
                },
                {
                    name: "成交量C",
                    data: [100,80,95,150,112,132]
                }
            ]
        };
        showCharts('kcbLUVWsggIzGDtPSuxQAVVwPDjOdJec', res);
    }, 300);
}
// test env
if (location.href.startsWith('http')) {
    getServerData();
}



